package JSONTools

import (
	"encoding/json"
	"fmt"
)

func Json2Map(jsonstr string)(map[string]interface{}){
	m := make(map[string]interface{})
	err := json.Unmarshal([]byte(jsonstr),&m)
	if(err!=nil){
		panic(err)
		return nil
	}
	return m
}

func Map2Json(m map[string]interface{})(string){
	return Interface2Sson(&m)
}

func Interface2Sson(m interface{})(string){
	bytes,err := json.Marshal(&m)
	if(err!=nil){
		panic(err)
	}
	return string(bytes)
}

type TStruct struct{
	Name string
	Age int
}

func main(){
	jsonstr := "{\"device\": \"1\",\"data\": [{\"humidity\": \"27\",\"time\": \"2017-07-03 15:23:12\"}]}"
	var m = Json2Map(jsonstr)
	fmt.Println(m)
	var s = Map2Json(m)
	fmt.Println(s)

	var t1 = TStruct{Name:"T1",Age:11}
	var t2 = new(TStruct)
	t2.Name="T2"
	t2.Age=22
	fmt.Println(Interface2Sson(&t1))
	fmt.Println(Interface2Sson(&t2))

}
